package com.ytkj.digitalConstruction.socketio;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.druid.util.StringUtils;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.listener.ConnectListener;
import com.ytkj.digitalConstruction.redis.RedisHashService;
import com.ytkj.digitalConstruction.util.SpringUtil;

/**
 * 连接建立监听器
 * 
 * @author momei
 *
 */
public class MyConnectListener implements ConnectListener{
	Logger logger = LoggerFactory.getLogger(getClass());
	
	@SuppressWarnings("unchecked")
	private RedisHashService<ClientInfo> redisHashClientInfoService = SpringUtil.getBean(RedisHashService.class);
	
	@Override
	public void onConnect(SocketIOClient client) {
		logger.info("conn ...");
		//获取客户端标示
		String clientId = client.getHandshakeData().getSingleUrlParam("clientId");
		logger.info("clientId:" + clientId);
		if(StringUtils.isEmpty(clientId) || clientId.equals("undefined")) {
			return;
		}
		//判断Redis中是否已加入当前客户端
		if(redisHashClientInfoService.get("client", clientId) == null) {
			//新增一个客户端信息
			ClientInfo clientInfo = new ClientInfo();
			clientInfo.setClientId(clientId);
			clientInfo.setLeastSignBits(client.getSessionId().getLeastSignificantBits());
			clientInfo.setMostSignBits(client.getSessionId().getMostSignificantBits());
			redisHashClientInfoService.put("client", clientId, clientInfo,-1);
		}
				
	}

}
